using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace QNH.CustomActions.Features.QNH_Custom_Workflow_Actions
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("98f75142-50a5-4e43-a932-f33833a0df1e")]
    public class QNH_Custom_Workflow_ActionsEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null)
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                site = web.Site;
            }

            SPWebApplication webApp = site.WebApplication;
            SPWebConfigModification authorizedTypeActivity = GetAuthorizedTypeConfigNode("QNH.ActivityLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5d9171eec465aaf6", "QNH.ActivityLibrary");

            if (authorizedTypeActivity != null)
            {
                webApp.WebConfigModifications.Add(authorizedTypeActivity);
                webApp.Update(true);
                webApp.WebService.ApplyWebConfigModifications();
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        private SPWebConfigModification GetAuthorizedTypeConfigNode(string assembly, string assemblyNamespace)
        {
            SPWebConfigModification newAuthorizedType = null;

            try
            {
                newAuthorizedType = new SPWebConfigModification();

                newAuthorizedType.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                newAuthorizedType.Name = string.Format("authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']", assembly, assemblyNamespace);
                newAuthorizedType.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";
                newAuthorizedType.Owner = assemblyNamespace;
                newAuthorizedType.Sequence = 0;
                newAuthorizedType.Value = string.Format("<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True' />", assembly, assemblyNamespace);
            }
            catch (Exception ex)
            {
                // Log error
            }

            return newAuthorizedType;
        }
    }
}
